package com.qingke.easyjava.jdbc.command.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.codec.digest.DigestUtils;

import com.qingke.easyjava.jdbc.app.BeSuperApplication;
import com.qingke.easyjava.jdbc.app.BeSuperDao;
import com.qingke.easyjava.jdbc.command.SystemCommand;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class LoginCommand extends SystemCommand {	//SystemCommand 实现接口 command,

    @Override
    public void execute() {							
        BeSuperDao dao = new BeSuperDao();			//一、
        
        String username = QingkeConsole.askUserInput("请输入用户名");
        // check username 
        while (!dao.isUsernameExists(username)) {	//二、
            QingkeConsole.println("用户名不存在！");
            return;
            
        }

        String password = QingkeConsole.askUserInput("请输入密码");
        
        Player player = dao.login(username, password);//三、通过用户名密码查找对应的id name score。可以尝试用验证密码和用户名的方法。
        
        if (player == null) {
            QingkeConsole.println("登录失败！用户名／密码不正确");
            return;
        }

        QingkeConsole.println("欢迎你回来," + player.getName());
        BeSuperApplication.getInstance().setPlayerProfile(player);//仅仅用于登录状态。
    }

}

// 一、
/* public Player login(String username, String password) {
        String sql = "SELECT id, name, score FROM player WHERE username = ? AND password = ?";
        
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);

            statement.setString(1, username);
            statement.setString(2, DigestUtils.md5Hex(password));
            
            rs = statement.executeQuery();
            
            Player player = null;
            while (rs.next()) {
                long id = rs.getLong("id");
                String name = rs.getString("name");
                int score = rs.getInt("score");
                player = new Player(id, name, score);
            }
            return player;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            cleanup(conn, statement, rs);
        }
    }
*/

//  二、
/*public boolean isUsernameExists(String username) {
        String sql = "select * from player where username = ?";
        
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);

            statement.setString(1, username);
            
            rs = statement.executeQuery();
            
            if (rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            cleanup(conn, statement, rs);
        }

        return false;
    }
*/

// 三、
/*public Player login(String username, String password) {
        String sql = "SELECT id, name, score FROM player WHERE username = ? AND password = ?";
        
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);

            statement.setString(1, username);
            statement.setString(2, DigestUtils.md5Hex(password));
            
            rs = statement.executeQuery();
            
            Player player = null;
            while (rs.next()) {
                long id = rs.getLong("id");
                String name = rs.getString("name");
                int score = rs.getInt("score");
                player = new Player(id, name, score);
            }
            return player;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            cleanup(conn, statement, rs);
        }
    }
*/